package com.bardsoftware.foronuvolo.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class BufferHttpResponse implements HttpServletResponse {
	private ByteArrayOutputStream myOutputStream = new ByteArrayOutputStream(1024);
	private HttpServletResponse myOriginalResponse;
	private PrintWriter myWriter; 
	public BufferHttpResponse(HttpServletResponse  original) {
		myOriginalResponse = original;
	}
	public ServletOutputStream getOutputStream() throws IOException {
		return new ServletOutputStream() {
			@Override
			public void write(int b) throws IOException {
				myOutputStream.write(b);
			}
		};
	}
	
	public PrintWriter getWriter() throws IOException {
		if (myWriter == null) {
		    myWriter = new PrintWriter(getOutputStream()) {
			@Override
			public void write(char[] arg0, int arg1, int arg2) {
    			super.write(arg0, arg1, arg2);
			}
		    };
		}
		return myWriter;
	}

	public String getResponse() {
		try {
			myWriter.flush();
			myWriter.close();
			myOutputStream.flush();
			myOutputStream.close();
			return myOutputStream.toString("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public void addCookie(Cookie arg0) {
	}
	public void addDateHeader(String arg0, long arg1) {
	}
	public void addHeader(String arg0, String arg1) {
	}
	public void addIntHeader(String arg0, int arg1) {
	}
	public boolean containsHeader(String arg0) {
		return myOriginalResponse.containsHeader(arg0);
	}
	public String encodeRedirectURL(String arg0) {
		return myOriginalResponse.encodeRedirectUrl(arg0);
	}
	public String encodeRedirectUrl(String arg0) {
		return null;
	}
	public String encodeURL(String arg0) {
		return null;
	}
	public String encodeUrl(String arg0) {
		return null;
	}
	public void sendError(int arg0) throws IOException {
	}
	public void sendError(int arg0, String arg1) throws IOException {
	}
	public void sendRedirect(String arg0) throws IOException {
	}
	public void setDateHeader(String arg0, long arg1) {
	}
	public void setHeader(String arg0, String arg1) {
	}
	public void setIntHeader(String arg0, int arg1) {
	}
	public void setStatus(int arg0) {
	}
	public void setStatus(int arg0, String arg1) {
	}
	public void flushBuffer() throws IOException {
		System.err.println("flush");
		myOutputStream.flush();
	}
	public int getBufferSize() {
		return myOutputStream.size();
	}
	public String getCharacterEncoding() {
		return myOriginalResponse.getCharacterEncoding();
	}
	public String getContentType() {
		return myOriginalResponse.getContentType();
	}
	public Locale getLocale() {
		return myOriginalResponse.getLocale();
	}
	public boolean isCommitted() {
		return myOriginalResponse.isCommitted();
	}
	public void reset() {
		myOutputStream.reset();
	}
	public void resetBuffer() {
		myOutputStream.reset();
	}
	public void setBufferSize(int arg0) {
		System.err.println("setBufferSize: "+arg0);
	}
	public void setCharacterEncoding(String arg0) {
	}
	public void setContentLength(int arg0) {
	}
	public void setContentType(String arg0) {
	}
	public void setLocale(Locale arg0) {
	}
}
